<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Str;
use Kuafu\File\FileUtil;

class DemoCommand extends Command
{
    protected $signature   = 'demo';
    protected $description = 'Command description';

    public function handle()
    {

    }

    /**
     * @param array $headers
     * @param array $data
     * @return void
     */
    public function export(array $headers, array $data)
    {
        // UTF8的BOM头
        $path = sys_get_temp_dir() . '/' . Str::random() . '.csv';
        $fd   = fopen($path, 'w+');
        fwrite($fd, "\xEF\xBB\xBF");

        // 表头
        if ($headers) {
            fputcsv($fd, $headers);
        }

        collect($data)->each(function ($item) use (&$fd) {
            fputcsv($fd, $item);
        });

        if (is_resource($fd)) {
            fclose($fd);
        }

        dump($path);

        $detailUrl = FileUtil::uploadFile($path, 'csv', "tmp/" . date('Ymd') . '/' . Str::uuid() . '.csv')['url'];
        dump($detailUrl);
    }
}

